package org.dromara.chicken.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.chicken.domain.ChickenChickIn;
import org.dromara.chicken.mapper.ChickenChickInMapper;
import org.dromara.chicken.service.IChickenChickInService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 鸡苗采购Service业务层处理
 *
 * @author Mr Lei QQ:235921049
 * @date 2024-06-12
 */
@RequiredArgsConstructor
@Service
public class ChickenChickInServiceImpl implements IChickenChickInService {

    private final ChickenChickInMapper baseMapper;

    /**
     * 查询鸡苗采购
     */
    @Override
    public ChickenChickIn queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询鸡苗采购列表
     */
    @Override
    public TableDataInfo<ChickenChickIn> queryPageList(ChickenChickIn bo, PageQuery pageQuery) {
        Page<ChickenChickIn> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询鸡苗采购列表
     */
    @Override
    public List<ChickenChickIn> queryList(ChickenChickIn bo) {
        LambdaQueryWrapper<ChickenChickIn> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ChickenChickIn> buildQueryWrapper(ChickenChickIn bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ChickenChickIn> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getHatchName()), ChickenChickIn::getHatchName, bo.getHatchName());
        lqw.eq(bo.getSupplierId() != null, ChickenChickIn::getSupplierId, bo.getSupplierId());
        return lqw;
    }

    /**
     * 新增鸡苗采购
     */
    @Override
    public Boolean insertByBo(ChickenChickIn bo) {
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    /**
     * 修改鸡苗采购
     */
    @Override
    public Boolean updateByBo(ChickenChickIn bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ChickenChickIn entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除鸡苗采购
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
